package cn.ztydata.teachingsystem.heaven.service;

import cn.ztydata.teachingsystem.exception.ServiceException;
import cn.ztydata.teachingsystem.heaven.common.WebContext;
import cn.ztydata.teachingsystem.heaven.dao.CourseOverviewDao;
import cn.ztydata.teachingsystem.heaven.entity.CourseProfile;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 课程概况服务类
 * Created by chenxin on 2014/12/10.
 */
@Service
public class CourseOverviewService extends BaseService {
    @Autowired
    private CourseOverviewDao courseOverviewDao;

    @Autowired
    private TeacherTeamService teacherTeamService;

    @Autowired
    private WebContext webContext;

    /**
     * 获取指定的课程概况内容
     * @param courseId 课程ID
     * @param type 概况类型
     * @return String
     *
     * @author cx
     * @since 2014-12-10
     */
    public CourseProfile getContent(final long courseId, final int type) {
        CourseProfile content = courseOverviewDao.getContent(courseId, type);

        if(content.getContent() != null){
            content.setContent(StringEscapeUtils.unescapeHtml4(content.getContent()));
        }

        log.debug("return 课程概况内容");
        return content;
    }

    /**
     * 编辑保存课程概况内容
     * @param courseId 课程ID
     * @param type 概况类型
     * @param content 概况内容
     *
     * @author cx
     * @since 2014-12-10
     */
    public void updateContent(final long courseId, final int type, String content) {
        //判断用户是否在课程教师团队
        teacherTeamService.isMemeber(courseId);

        if (!courseOverviewDao.updateContent(courseId, type, StringEscapeUtils.escapeHtml4(content))) {
            log.debug("更新课程概况内容失败:!courseOverviewDao.updateContent(courseId, type, content)");
            log.warn("更新课程概况内容失败:courseId->" + courseId);
            throw new ServiceException();
        }
    }

    /**
     * 编辑保存课程概况文件
     * @param courseId 课程ID
     * @param type 概况类型
     * @param url 文件url
     *
     * @author fanruiping
     * @since 2015-02-05
     */
    public void updateFile(final long courseId, final int type, String url) {
        //判断用户是否在课程教师团队
        teacherTeamService.isMemeber(courseId);

        if (!courseOverviewDao.updateFile(courseId, type, url)) {
            log.debug("更新课程概况文件失败:!courseOverviewDao.updateFile(courseId, type, url)");

            log.warn("更新课程概况文件失败:courseId->" + courseId);

            throw new ServiceException();
        }
    }

    /**
     * 清楚课程概况文件
     * @param courseId 课程编号
     * @param type 概况类型
     *
     * @author fanruiping
     * @since 2015-02-05
     */
    public void deleteFile(final long courseId, final int type){
        //判断用户是否在课程教师团队
        teacherTeamService.isMemeber(courseId);

        if (!courseOverviewDao.deleteFile(courseId, type)) {
            log.debug("删除课程概况文件失败:!courseOverviewDao.updateFile(courseId, type)");

            log.warn("删除课程概况文件失败:courseId->" + courseId);

            throw new ServiceException();
        }
    }

    /**
     * 获取概况是否为空
     * @param courseId 课程iD
     * @return List
     *
     * @author cx
     * @since 2014-12-17
     */
    public List<Long> getForNull(final long courseId) {
        Map<String, Object> nullTag = courseOverviewDao.getForNull(courseId);

        List<Long> result = Lists.newArrayList();
        if (nullTag == null) {
            log.debug("nullTag == null");
            return result;
        }

        log.debug("循环课程概况内容空标识");
        for (int i = 1; i <= 8; i++) {
            result.add((Long) nullTag.get(String.valueOf(i)));
        }

        log.debug("return 概况是否为空");
        return result;
    }
}
